package com.coding.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;

/**
 * -- Created by IntelliJ IDEA.
 * -- REMARK:
 * -- User: tanwei
 * -- Date: 2020/7/3
 * -- Time: 4:48 下午
 * --
 */
@Configuration
public class DruidConfing {


    // 绑定配置的bean
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

    //注册后台监控页面 springboot如何注册servlet
    //没有web.xml 的情况配置Servlet方法 ：  ServletRegistrationBean
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        HashMap<String, String> map = new HashMap<>();
        map.put("loginUsername", "root");
        map.put("loginPassword", "qazwsx123");
        map.put("allow", "");//所有人可以访问
//        map.put("deny","");//拒绝访问
        bean.setInitParameters(map);// 设置servlet初始化参数
        return bean;
    }


    //过滤器的配置,看看那些请求需要被过滤
    //没有web.xml 的情况配置Servlet方法 ：  FilterRegistrationBean
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        //配置内容
        //配置那些请求可以过滤
        HashMap<String, String> map = new HashMap<>();
        map.put("exclusions", "*js,*.css");
        bean.setInitParameters(map);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
